const express = require('express')
const router = express.Router()
const mysql = require("../mysql/config")
let connection = null
const resSend = require('../utils/resSend')

router.post("/add",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.body.title,req.body.Money,req.body.content,req.body.allCount]
    // 添加优惠券
    connection.query(`INSERT INTO coupon_table (title,Money,content,allCount) VALUES (?,?,?,?)`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

// 修改优惠券
router.post("/update",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params =  [req.body.title,req.body.Money,req.body.content,req.body.allCount,req.body.id]
    // 修改优惠券
    connection.query(`UPDATE coupon_table SET title=?,Money=?,content=?,allCount=? WHERE id=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

// 删除
router.post("/delete",(req,res)=>{
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.body.id]
    // 删除优惠券
    connection.query(`DELETE FROM coupon_table WHERE id=?`,params,(err,result)=>{
        if(err){
            resSend(res,500,"服务异常！")
        }else{
            resSend(res,200,'操作成功！')
        }
    })
})

module.exports = router